<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="物料类型" prop="classId">
        <el-select style="width: 100%" clearable v-model="queryParams.classId" placeholder="物料类型" >
          <el-option
            v-for="dict in classOptions"
            :key="dict.classId"
            :label="dict.className"
            :value="dict.classId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="产品编号" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料识别码" prop="itemIdentifyCode">
        <el-input
          v-model="queryParams.itemIdentifyCode"
          placeholder="请输入物料识别码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生成标记" prop="scFlag">
        <el-select   @keyup.enter.native="handleQuery"   v-model="queryParams.scFlag" clearable placeholder="请选择生成标记">
          <el-option
            v-for="dict in dict.type.sc_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:iteminfoGz:add']"
        >新增</el-button>
      </el-col>
    <!--  <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:iteminfoGz:edit']"
        >修改</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleAddItemInfo"
          v-hasPermi="['base:iteminfoGz:addItemInfo']"
        >生成物料明细</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:iteminfoGz:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:iteminfoGz:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-printer"
          size="mini"
          :disabled="single"
          @click="printTm"
        >打印条码</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table height="calc(100vh - 300px)" v-loading="loading" :data="iteminfoGzList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公司代码" align="center" prop="tenantId" />
      <el-table-column label="类型代码" align="center" prop="classId"/>
      <el-table-column label="物料类型" width="180" show-overflow-tooltip align="center" prop="className" />
      <el-table-column label="规格代码" width="120" align="center" prop="specCode" />
      <el-table-column label="规格型号" width="120" align="center" prop="spec" />
      <el-table-column label="物料单位" width="100" align="center" prop="unit">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jc_iteminfo_unit" :value="scope.row.unit"/>
        </template>
      </el-table-column>
      <el-table-column label="物料识别码" width="220" show-overflow-tooltip align="center" prop="itemIdentifyCode" />
      <el-table-column width="120" label="标准名称" align="center" prop="itemBzName" />
      <el-table-column width="120" label="产品编号" align="center" prop="itemCode" />
      <el-table-column width="120" label="产品名称" align="center" prop="itemName" />
      <el-table-column label="顺序号" align="center" prop="seq" />
      <el-table-column label="名称构成" align="center" prop="nameGc">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gz_name" :value="scope.row.nameGc"/>
        </template>
      </el-table-column>
      <el-table-column label="条码号" align="center" prop="tmh" />
     <!-- <el-table-column label="一维码" align="center" prop="oneCode" />
      <el-table-column label="二维码" align="center" prop="twoCode" />
      <el-table-column label="rfid码" align="center" prop="rfidCode" />-->
      <el-table-column label="生成标记" align="center" prop="scFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sc_flag" :value="scope.row.scFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:iteminfoGz:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:iteminfoGz:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料生成规则对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司代码" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入公司代码" />
        </el-form-item>
        <el-form-item label="物料类型"  prop="classId">
          <el-select multiple  @change="handleGetClass" style="width: 100%" filterable v-model="form.classId" placeholder="大类名称" >
            <el-option
              v-for="dict in classOptions"
              :key="dict.classId"
              :label="dict.className"
              :value="dict.classId"
            />
          </el-select>
        </el-form-item>
      <!--  <el-form-item label="物料类型" prop="className">
          <el-input v-model="form.className" placeholder="请输入物料类型" />
        </el-form-item>-->
        <el-form-item label="规格代码" prop="specCode">
          <el-input v-model="form.specCode" placeholder="请输入规格代码" />
        </el-form-item>
        <el-form-item label="规格型号" prop="spec">
          <el-input v-model="form.spec" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="物料单位" prop="unit">
          <el-select style="width:100%" clearable v-model="form.unit" placeholder="请选择物料单位">
            <el-option
              v-for="dict in dict.type.jc_iteminfo_unit"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
       <!-- <el-form-item label="顺序号" prop="seq">
          <el-input v-model="form.seq" placeholder="请输入顺序号" />
        </el-form-item>-->
        <el-form-item label="名称构成" prop="nameGc">
          <el-select v-model="form.nameGc" clearable placeholder="请选择名称构成">
            <el-option
              v-for="dict in dict.type.gz_name"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生成标记" prop="scFlag">
          <el-select style="width:100%" clearable v-model="form.scFlag" placeholder="请选择生成标记">
            <el-option
              v-for="dict in dict.type.sc_flag"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--<el-form-item label="产品编号" prop="itemCode">
          <el-input v-model="form.itemCode" placeholder="请输入产品编号" />
        </el-form-item>
        <el-form-item label="产品名称" prop="itemName">
          <el-input v-model="form.itemName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="条码号" prop="tmh">
          <el-input v-model="form.tmh" placeholder="请输入条码号" />
        </el-form-item>
        <el-form-item label="一维码" prop="oneCode">
          <el-input v-model="form.oneCode" placeholder="请输入一维码" />
        </el-form-item>
        <el-form-item label="二维码" prop="twoCode">
          <el-input v-model="form.twoCode" placeholder="请输入二维码" />
        </el-form-item>
        <el-form-item label="rfid码" prop="rfidCode">
          <el-input v-model="form.rfidCode" placeholder="请输入rfid码" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listIteminfoGz, getIteminfoGz, delIteminfoGz, addIteminfoGz, updateIteminfoGz,addItemInfo } from "@/api/base/iteminfoGz";
  import { getListClass} from "@/api/base/class";
  import {getLodop} from "@/common/LodopFuncs";

  export default {
    name: "IteminfoGz",
    dicts: ['gz_name','jc_iteminfo_unit','sc_flag'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 物料生成规则表格数据
        iteminfoGzList: [],
        //大类单位字典
        classOptions: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          classId: null,
          nameGc: null,
          itemCode: null,
          itemName: null,
          itemIdentifyCode: null,
          scFlag: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          tenantId: [
            { required: true, message: "公司代码不能为空", trigger: "blur" }
          ],
          classId: [
            { required: true, message: "类型代码不能为空", trigger: "change" }
          ],
          specCode: [
            { required: true, message: "规格代码不能为空", trigger: "blur" }
          ],
          spec: [
            { required: true, message: "规格型号不能为空", trigger: "blur" }
          ],
        /*  nameGc: [
            { required: true, message: "名称构成不能为空", trigger: "blur" }
          ],*/
        }
      };
    },
    created() {
      this.getList();
      getListClass().then(response => {
        this.classOptions = response.data;
      });
    },
    methods: {
      printTm(){
        this.reset();
        const id = this.ids[0]
        getIteminfoGz(id).then(res=>{
          let data = res.data
          let tmh = data.tmh
          let itemName = data.itemName
          let itemCode = data.itemCode
          console.log(data,'ssss')
          let LODOP = getLodop();
          LODOP.PRINT_INIT("");
          LODOP.PRINT_INITA(0,0,190,94,"");
          LODOP.SET_PRINT_PAGESIZE(1,500,250,"");
          LODOP.ADD_PRINT_BARCODE(21,2,87,134,"QRCode",tmh);
          // LODOP.ADD_PRINT_TEXT(9,70,100,20,"条码号:"+tmh);
          LODOP.ADD_PRINT_TEXT(20,70,100,20,"产品名:");
          LODOP.ADD_PRINT_TEXT(35,70,100,20,itemName);
          LODOP.ADD_PRINT_TEXT(55,70,100,20,"产品码:");
          LODOP.ADD_PRINT_TEXT(70,70,100,20,itemCode);
          //    LODOP.PRINT();
          LODOP.PREVIEW();
          // LODOP.PRINT_DESIGN()
        })
        // console.log(id)
      },
      /** 查询物料生成规则列表 */
      getList() {
        this.loading = true;
        listIteminfoGz(this.queryParams).then(response => {
          this.iteminfoGzList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          tenantId: "TK",
          classId: null,
          className: null,
          specCode: null,
          spec: null,
          seq: null,
          nameGc: null,
          itemCode: null,
          itemName: null,
          tmh: null,
          oneCode: null,
          twoCode: null,
          rfidCode: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handleGetClass(value){
        let opt= {};
        opt= this.classOptions.find((item)=>{
          return item.classId === value;
        });
        this.form.className=opt.className;
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加物料生成规则";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getIteminfoGz(id).then(response => {
          this.form = response.data;
          this.form.classId=this.form.classId.split(',');
          this.open = true;
          this.title = "修改物料生成规则";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.classId=this.form.classId.toString();
            if (this.form.id != null) {
              updateIteminfoGz(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addIteminfoGz(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      handleAddItemInfo(row){
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认生成物料信息？').then(function() {
          return addItemInfo(ids);
        }).then(() => {
          this.getList();
          this.$modal.alertSuccess("生成物料信息成功");
        }).catch(() => {});
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除物料生成规则编号为"' + ids + '"的数据项？').then(function() {
          return delIteminfoGz(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('base/iteminfoGz/export', {
          ...this.queryParams
        }, `iteminfoGz_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
